package com.yh.concert.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yh.concert.domain.YhOrder;
import com.yh.concert.mapper.YhOrderMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yh.concert.mapper.YhConcertMapper;
import com.yh.concert.domain.YhConcert;
import com.yh.concert.service.IYhConcertService;

import javax.annotation.Resource;

/**
 * 演唱会管理Service业务层处理
 *
 * @author yh
 * @date 2025-02-26
 */
@Service
public class YhConcertServiceImpl extends ServiceImpl<YhConcertMapper, YhConcert> implements IYhConcertService {

    @Resource
    private YhConcertMapper yhConcertMapper;

    @Resource
    private YhOrderMapper yhOrderMapper;

    /**
     * 查询演唱会管理列表
     *
     * @param yhConcert 演唱会管理
     * @return 演唱会管理
     */
    @Override
    public List<YhConcert> selectYhConcertList(YhConcert yhConcert)
    {
        List<YhConcert> yhConcertList = yhConcertMapper.selectList(buildQueryWrapper(yhConcert));
        return yhConcertList;
    }


    private LambdaQueryWrapper<YhConcert> buildQueryWrapper(YhConcert query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<YhConcert> lqw = Wrappers.lambdaQuery();
        lqw.eq(query.getMerchartId() != null, YhConcert::getMerchartId, query.getMerchartId());
        lqw.eq(StringUtils.isNotBlank(query.getConcertName()), YhConcert::getConcertName, query.getConcertName());
        lqw.eq(query.getUserId() != null, YhConcert::getUserId, query.getUserId());
        lqw.eq(StringUtils.isNotBlank(query.getIntro()), YhConcert::getIntro, query.getIntro());
        lqw.eq(query.getAddress() != null, YhConcert::getAddress, query.getAddress());
        lqw.eq(query.getMoney() != null, YhConcert::getMoney, query.getMoney());
        lqw.eq(query.getCount() != null, YhConcert::getCount, query.getCount());
        lqw.eq(query.getStatus() != null, YhConcert::getStatus, query.getStatus());
        lqw.orderByDesc(YhConcert::getCreateTime);
        return lqw;
    }

    /**
     * 查询订单量最大的7场演唱会
     *
     * @return 订单量最大的7场演唱会列表
     */
    @Override
    public Map getTop7ConcertsByOrderCount() {

        // 获取未开始的所有演唱会ID
        // 查询开始时间还没开始的演唱会
        List<YhConcert> yhConcerts = yhConcertMapper.selectList(new LambdaQueryWrapper<YhConcert>().ge(YhConcert::getStartTime, new Date()));
        Map<String, Long> concertCountByCount = new HashMap<>();
        // 根据演唱会ID，统计每个演唱会的订单数量，并排序取前7个
        for (YhConcert yhConcert : yhConcerts){
            Long concertId = yhConcert.getId();
            Long orderCount = yhOrderMapper.selectCount(new LambdaQueryWrapper<YhOrder>().eq(YhOrder::getConcertId, concertId));
            concertCountByCount.put(yhConcert.getConcertName(), orderCount);
        }
        return concertCountByCount;
    }
}
